package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.ProductConstants;
import com.powernode.domain.Category;
import com.powernode.ex.handler.BusinessException;
import com.powernode.mapper.CategoryMapper;
import com.powernode.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    @Cacheable(key = ProductConstants.SYS_ALL_CATEGORY)
    public List<Category> queryProdCategoryList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .orderByDesc(Category::getSeq)
        );
    }

    @Override
    @Cacheable(key = ProductConstants.SYS_FIRST_CATEGORY)
    public List<Category> queryCategoryListByPid(Long pid) {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getStatus,1)
                .eq(Category::getParentId,pid)
                .orderByDesc(Category::getSeq)
        );
    }

    @Override
    @Caching(evict = {
        @CacheEvict(key = ProductConstants.SYS_ALL_CATEGORY),
        @CacheEvict(key = ProductConstants.SYS_FIRST_CATEGORY)
    })
    public Integer saveCategory(Category category) {
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        return categoryMapper.insert(category);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.SYS_ALL_CATEGORY),
            @CacheEvict(key = ProductConstants.SYS_FIRST_CATEGORY)
    })
    public Integer modifyCategory(Category category) {
        // 根据标识查询类目之前的级别
        Category oldCategory = categoryMapper.selectById(category.getCategoryId());
        // 获取商品类目的父节点标识
        Long parentId = category.getParentId();

        // 2->1 : 原有的类目的pid不为0 且 修改的类目父节点id为null
        if (ObjectUtil.isNull(parentId) && oldCategory.getParentId() != 0) {
            category.setParentId(0L);
        }
        // 1->2 : 原有的类目的pid为0 且 修改的类目父节点id不为0
        if (ObjectUtil.isNotNull(parentId) && parentId != 0 && oldCategory.getParentId() == 0) {
            // 查询当前一级类目是否包含子类目
            List<Category> child = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                    .eq(Category::getParentId, category.getCategoryId())
            );
            if (CollectionUtil.isNotEmpty(child) && child.size() != 0) {
                // 说明：当前一级类目包含子类目，所以不可修改
                throw new BusinessException("当前一级类目包含子类目，不可修改");
            }
        }
        category.setUpdateTime(new Date());
        return categoryMapper.updateById(category);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.SYS_ALL_CATEGORY),
            @CacheEvict(key = ProductConstants.SYS_FIRST_CATEGORY)
    })
    public Boolean removeCategoryById(Long categoryId) {
        // 删除当前类目的子类目
        categoryMapper.delete(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId,categoryId)
        );
        return categoryMapper.deleteById(categoryId)>0;
    }

    @Override
    public List<Category> queryWxCategoryList(Long parentId) {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId,parentId)
                .eq(Category::getStatus,1)
                .orderByDesc(Category::getSeq)
        );
    }
}
